(function(){
    app.controller('datacenterTabCtrl', ['$scope', 'httpLoad', '$rootScope','$modal','$state','$timeout',
        function($scope, httpLoad, $rootScope, $modal,$state, $timeout) {
            $rootScope.moduleTitle = '资源管理 > 基础设施';//定义当前页
            $rootScope.link = '/statics/css/tpl-card.css';
            sessionStorage.clear();

            $scope.active1 = true;$scope.active2 = $scope.active3 = false;
        }
    ]);
    app.controller('datacenterCtrl', ['$scope', 'httpLoad', '$rootScope','$modal','$state','$timeout','$stateParams',
        function($scope, httpLoad, $rootScope, $modal,$state, $timeout,$stateParams) {
            $scope.param = {
                rows: 10
            };
            $scope.showDetail = false;
            //获取列表
            $scope.getData = function(page){
                $scope.param.page = page || $scope.param.page;
                var params = {
                        page: $scope.param.page,
                        rows: $scope.param.rows
                    },
                    searchParam = [];
                if($scope.searchByName&&$scope.searchByName!=""){
                    searchParam.push({"param":{"name":$scope.searchByName},"sign":"LK"});
                }
                params.params = JSON.stringify(searchParam);
                httpLoad.loadData({
                    url:'/dc/list',
                    method: 'POST',
                    data: params,
                    noParam: true,
                    success:function(data){
                        if(data.success){
                            $scope.dataCenterList = data.data.rows;
                            $scope.totalCount = data.data.total;
                            if(data.data.rows.length==0) $scope.isImageData = true;
                            else $scope.isImageData = false;
                        }else{
                            $scope.isImageData = true;
                        }
                    }
                });
            };
            $scope.getData(1);

            //重置
            $scope.reset = function(){
                $scope.searchByName = "";
            };
            //新增
            $scope.add = function ($event, size) {  //打开模态
                $event.stopPropagation();
                if ($scope.dataCenterList) {
                    var length = $scope.dataCenterList.length || 0;
                    if (length >= 10) {
                        $scope.pop('数据中心数量已达上限10个，无法添加', 'error');
                        return;
                    }
                }
                var modalInstance = $modal.open({
                    templateUrl: '/statics/tpl/assets/datacenter/add.html',  //指向上面创建的视图
                    controller: 'addDataCenterModalCtrl1',// 初始化模态范围
                    size: size, //大小配置
                    backdrop: 'static',
                    resolve: {
                        dataList: function () {
                            return $scope.dataCenterList;
                        }
                    }
                });
                modalInstance.result.then(function (result) {
                    $scope.getData();
                });
            };
            //编辑
            $scope.update = function ($event, id) {  //打开模态
                $event.stopPropagation();
                httpLoad.loadData({
                    url: '/dc/basic',
                    method: 'GET',
                    data: {id: id},
                    success: function (data) {
                        if (data.success) {
                            var modalInstance = $modal.open({
                                templateUrl: '/statics/tpl/assets/datacenter/update.html',  //指向上面创建的视图
                                controller: 'updateDataCenterModalCtrl1',// 初始化模态范围
                                backdrop: 'static',
                                resolve: {
                                    id: function () {
                                        return id;
                                    },
                                    updateData: function () {
                                        return data.data;
                                    }
                                }
                            });
                            modalInstance.result.then(function () {
                                $scope.getData();
                            });
                        }
                    }
                });
            };
            //删除
            $scope.remove = function ($event, id) {  //打开模态
                $event.stopPropagation();
                var modalInstance = $modal.open({
                    templateUrl: '/statics/tpl/assets/datacenter/remove.html',  //指向上面创建的视图
                    controller: 'removeDataCenterModalCtrl',// 初始化模态范围
                    resolve: {
                        id: function () {
                            return id;
                        }
                    }
                });
                modalInstance.result.then(function () {
                    $scope.getData();
                });
            };
            //返回
            $scope.goBack = function(){
                $scope.isActive = false;
                $timeout(function() {
                    $scope.showDetail = false;
                }, 200);
            };
            //跳转详情页
            $scope.detail = function ($event,id) {
                $event.stopPropagation();
                httpLoad.loadData({
                    url: '/dc/detail',
                    method: 'GET',
                    data: {id: id},
                    success: function(data){
                        if(data.success){
                            $scope.centerDetail = data.data;
                            $scope.showDetail = $scope.isActive = true;
                        }
                    }
                });
            };
        }
    ]);
    app.controller('updateDataCenterModalCtrl1', ['$scope', '$modalInstance', 'httpLoad', '$http','LANGUAGE', 'updateData',
        function ($scope, $modalInstance, httpLoad, $http, LANGUAGE, updateData) { //依赖于modalInstance
            var aa = JSON.stringify(updateData);
            $scope.updateData = JSON.parse(aa);
            $scope.updateData.totalArea = parseInt($scope.updateData.totalArea);
            $scope.today = new Date();
            $http.get('/statics/api/map/map.json').then(function(data){
                $scope.chinaMap = data.data;
                //拉去默认城市数据
                $scope.chinaMap.forEach(function (item) {
                    if($scope.updateData.province == item.name){
                        $scope.provinceItem = item;
                        $scope.cityData = item.children;
                        return false;
                    }
                })
                $scope.cityData.forEach(function (item) {
                    if($scope.updateData.city == item.name){
                        $scope.cityItem = item;
                        return false;
                    }
                });
            });
            $scope.selectProvince = function () {
                $scope.cityData = JSON.parse($scope.provinceItem).children;
            };
            $scope.ok = function () {
                //获取坐标
                $scope.updateData.province = angular.fromJson($scope.provinceItem).name;
                var cityItem = angular.fromJson($scope.cityItem);
                $scope.updateData.city = cityItem.name;
                $scope.updateData.coordinate = cityItem.log+','+cityItem.lat;
                httpLoad.loadData({
                    url: '/dc/modify',
                    data: $scope.updateData,
                    success: function (data) {
                        if (data.success) {
                            $scope.updateResult = $scope.updateData;
                            $scope.pop(LANGUAGE.ASSETS.CENTER.EDIT_SUCCESS);
                            $modalInstance.close();
                        }
                    }
                });
            };
            $scope.cancel = function () {
                $modalInstance.dismiss('cancel'); // 退出
            }
    }]);
    app.controller('addDataCenterModalCtrl1', ['$scope','$rootScope', '$modalInstance','$http', 'httpLoad', 'LANGUAGE', 'dataList',
        function ($scope,$rootScope, $modalInstance,$http, httpLoad, LANGUAGE, dataList) { //依赖于modalInstance
            $rootScope.link = '/statics/css/tpl-card.css';
            $http.get('/statics/api/map/map.json').then(function(data){
                $scope.chinaMap = data.data;
            });
            $scope.selectProvince = function () {
                $scope.cityData = JSON.parse($scope.provinceItem).children;
            };
            $scope.today = new Date();
            $scope.isSame = false;
            $scope.foucs = function () {
                $scope.isSame = false;
                $scope.adddataCenterForm.$invalid = false;
            };
            $scope.blur = function () {
                $scope.isSame = false;
                $scope.adddataCenterForm.$invalid = false;
            };

            $scope.ok = function () {
                for (var a in dataList) {
                    if (dataList[a].name == $scope.addData.name) {
                        $scope.isSame = true;
                        $scope.adddataCenterForm.$invalid = true;
                    }
                }
                //获取坐标
                $scope.addData.province = JSON.parse($scope.provinceItem).name;
                var cityItem = JSON.parse($scope.cityItem);
                $scope.addData.city = cityItem.name;
                $scope.addData.coordinate = cityItem.log+','+cityItem.lat;
                if (!$scope.isSame) {
                    httpLoad.loadData({
                        url: '/dc/create',
                        data: $scope.addData,
                        success: function (data) {
                            if (data.success) {
                                $scope.pop(LANGUAGE.ASSETS.CENTER.ADD_SUCCESS);
                                $modalInstance.close();
                            }
                        }
                    });
                }
            };

            $scope.cancel = function () {
                $modalInstance.dismiss('cancel'); // 退出
            }
    }]);
    app.controller('removeDataCenterModalCtrl', ['$scope', '$modalInstance', 'httpLoad', 'LANGUAGE', 'id',
        function ($scope, $modalInstance, httpLoad, LANGUAGE, id) { //依赖于modalInstance
            $scope.ok = function () {
                httpLoad.loadData({
                    url: '/dc/remove',
                    data: {id: id},
                    success: function (data) {
                        if (data.success) {
                            $scope.pop(LANGUAGE.ASSETS.CENTER.DEL_SUCCESS);
                            $modalInstance.close();
                        }
                    }
                });
            };
            $scope.cancel = function () {
                $modalInstance.dismiss('cancel'); // 退出
            }
    }]);
    app.controller('roomCtrl', ['$scope', 'httpLoad', '$rootScope','$modal','$state','$timeout','$stateParams',
        function($scope, httpLoad, $rootScope, $modal,$state, $timeout,$stateParams) {
            $scope.param = {
                rows: 10
            };
            $scope.showDetail = false;
            //获取列表
            $scope.getData = function(page){
                $scope.param.page = page || $scope.param.page;
                var params = {
                        page: $scope.param.page,
                        rows: $scope.param.rows
                    },
                    searchParam = [];
                if($scope.searchByName&&$scope.searchByName!=""){
                    searchParam.push({"param":{"name":$scope.searchByName},"sign":"LK"});
                }
                params.params = JSON.stringify(searchParam);
                httpLoad.loadData({
                    url:'/room/list',
                    method: 'POST',
                    data: params,
                    noParam: true,
                    success:function(data){
                        if(data.success&&data.data.rows){
                            $scope.roomListData = data.data.rows;
                            $scope.totalCount = data.data.total;
                            if(data.data.rows.length==0) $scope.isImageData = true;
                            else $scope.isImageData = false;
                        }else{
                            $scope.isImageData = true;
                        }
                    }
                });
            };

            //重置
            $scope.reset = function(){
                $scope.searchByName = "";
            };
            //添加/编辑机房
            $scope.update = function ($event,id,dcId) {
                $event.stopPropagation();
                if ($scope.roomListData) {
                    var length = $scope.roomListData.length || 0;
                    if (length >= 10) {
                        $scope.pop('机房数量已达上限10个，无法添加', 'error');
                        return;
                    }
                }
                var modalInstance = $modal.open({
                    templateUrl: '/statics/tpl/assets/room/addRoomModal.html',
                    controller: 'addRoomModalCtrl1',
                    backdrop: 'static',
                    resolve: {
                        id: function () {
                            return id || '';
                        },
                        dcId: function () {
                            return dcId|| '';
                        }
                    }
                });
                modalInstance.result.then(function (data) {
                    $scope.getData();
                });
            };
            //删除机柜
            $scope.remove = function ($event, id) {
                $event.stopPropagation();
                var modalInstance = $modal.open({
                    templateUrl: '/statics/tpl/assets/room/delRoomModal.html',
                    controller: 'delRoomModalCtrl',
                    backdrop: 'static',
                    resolve: {
                        id: function () {
                            return id;
                        }
                    }
                });
                modalInstance.result.then(function () {
                    $scope.getData();
                });
            };
            //返回
            $scope.goBack = function(){
                $scope.isActive = false;
                $timeout(function() {
                    $scope.showDetail = false;
                }, 200);
            };
            //跳转详情页
            $scope.detail = function ($event,id) {
                $event.stopPropagation();
                httpLoad.loadData({
                    url: '/room/detail',
                    method: 'GET',
                    data: {id: id},
                    success: function(data){
                        if(data.success){
                            $scope.roomDetail = data.data;
                            $scope.showDetail = $scope.isActive = true;
                        }
                    }
                });
            };
        }
    ]);
    //添加/编辑机房ctrl
    app.controller('addRoomModalCtrl1', ['$scope', '$modalInstance', '$stateParams', 'httpLoad','id','dcId',
        function ($scope, $modalInstance, $stateParams, httpLoad, id,dcId) {
            $scope.modalName = '添加机房';
            $scope.today = new Date();
            if (dcId) {
                $scope.dcId = dcId;
            }
            var editObj = ['dcId', 'locationSn', 'totalArea', 'contacter', 'name', 'remark', 'contactPhone', 'type', 'rowNum', 'colNum', 'commissionDate'];
            var url = '/room/create';
            //获取数据中心列表数据
            httpLoad.loadData({
                url: '/dc/list',
                noParam: true,
                success: function (data) {
                    if (data.success) {
                        $scope.dataCenterData = data.data.rows;
                    }
                }
            });
            //获取机柜类型列表数据
            httpLoad.loadData({
                url: '/dict/children',
                method: 'GET',
                data: {
                    value: "ROOM_CATEGORY",
                },
                success: function (data) {
                    if (data.success) {
                        $scope.dataType = data.data;
                    }
                }
            });

            //如果为编辑，进行赋值
            if (id) {
                url = '/room/modify';
                $scope.modalName = '编辑机房';
                httpLoad.loadData({
                    url: '/room/basic',
                    method: 'GET',
                    data: {id: id},
                    success: function (data) {
                        if (data.success) {
                            var data = data.data;
                            for (var a in editObj) {
                                var attr = editObj[a];
                                $scope[attr] = data[attr];
                            }
                            $scope.totalArea = parseInt($scope.totalArea);
                            // $scope.rowNum = parseInt($scope.rowNum);
                            // $scope.colNum = parseInt($scope.colNum);
                        }
                    }
                });
            }
            //保存按钮
            $scope.ok = function () {
                var param = {};
                for (var a in editObj) {
                    var attr = editObj[a];
                    param[attr] = $scope[attr];
                }
                if (id) param.id = id;
                httpLoad.loadData({
                    url: url,
                    data: param,
                    success: function (data) {
                        if (data.success) {
                            var popText = '机房添加成功';
                            if (id) popText = '机房修改成功';
                            $scope.pop(popText);
                            $modalInstance.close(data);
                        }
                    }
                });
            }
            $scope.cancle = function () {
                $modalInstance.dismiss('cancel');
            };
        }
    ]);
    //删除机房ctrl
    app.controller('delRoomModalCtrl', ['$scope', '$modalInstance', 'httpLoad', 'id',
        function ($scope, $modalInstance, httpLoad, id) {
            $scope.ok = function () {
                httpLoad.loadData({
                    url: '/room/remove',
                    data: {id: id},
                    success: function (data) {
                        if (data.success) {
                            $scope.pop("机房删除成功");
                            $modalInstance.close();
                        }
                    }
                });
            }
            $scope.cancle = function () {
                $modalInstance.dismiss('cancel');
            };
        }
    ]);
    app.controller('rackCtrl', ['$scope', 'httpLoad', '$rootScope','$modal','$state','$timeout','$stateParams',
        function($scope, httpLoad, $rootScope, $modal,$state, $timeout,$stateParams) {
            $scope.param = {
                rows: 10
            };
            $scope.showDetail = false;
            //获取列表
            $scope.getData = function(page){
                $scope.param.page = page || $scope.param.page;
                var params = {
                        page: $scope.param.page,
                        rows: $scope.param.rows
                    },
                    searchParam = [];
                if($scope.searchByName&&$scope.searchByName!=""){
                    searchParam.push({"param":{"name":$scope.searchByName},"sign":"LK"});
                }
                params.params = JSON.stringify(searchParam);
                httpLoad.loadData({
                    url:'/rack/list',
                    method: 'POST',
                    data: params,
                    noParam: true,
                    success:function(data){
                        if(data.success&&data.data.rows){
                            $scope.cabinetListData = data.data.rows;
                            $scope.totalCount = data.data.total;
                            if(data.data.rows.length==0) $scope.isImageData = true;
                            else $scope.isImageData = false;
                        }else{
                            $scope.isImageData = true;
                        }
                    }
                });
            };

            //添加/编辑机柜
            $scope.addCabinet = function($event,id,dcId,roomId){
                $event.stopPropagation();
                var modalInstance = $modal.open({
                    templateUrl: '/statics/tpl/assets/rack/addCabinetModal.html',
                    controller: 'addCabinetModalCtrl1',
                    backdrop: 'static',
                    size: 'lg',
                    resolve: {
                        id: function() {
                            return id;
                        },
                        dcId: function() {
                            return dcId;
                        },
                        roomId: function() {
                            return roomId;
                        }
                    }
                });
                modalInstance.result.then(function(data) {
                    $scope.getData();
                });
            };
            //删除机柜
            $scope.delCabinet = function($event,id){
                $event.stopPropagation();
                var modalInstance = $modal.open({
                    templateUrl: '/statics/tpl/assets/rack/delCabinetModal.html',
                    controller: 'delCabinetModalCtrl',
                    backdrop: 'static',
                    resolve: {
                        id: function() {
                            return  id;
                        }
                    }
                });
                modalInstance.result.then(function() {
                    $scope.getData();
                    //$scope.roomDetail.racks -= 1;
                });
            };
            //返回
            $scope.goBack = function(){
                $scope.isActive = false;
                $timeout(function() {
                    $scope.showDetail = false;
                }, 200);
            };
            //跳转详情页
            $scope.detail = function ($event,id) {
                $event.stopPropagation();
                httpLoad.loadData({
                    url: '/rack/detail',
                    method: 'GET',
                    data: {id: id},
                    success: function(data){
                        if(data.success){
                            $scope.rackDetail = data.data;
                            $scope.showDetail = $scope.isActive = true;
                        }
                    }
                });
            };
        }
    ]);
    //添加/编辑机柜ctrl
    angular.module('app').controller('addCabinetModalCtrl1', ['$scope', '$modalInstance', '$stateParams', 'httpLoad', 'LANGUAGE','id','dcId','roomId',
        function($scope, $modalInstance, $stateParams,  httpLoad, LANGUAGE, id,dcId,roomId) {
            $scope.modalName = '添加机柜';
            $scope.today = new Date();
            $scope.inpit_Y= true;
            //获取数据中心列表数据
            httpLoad.loadData({
                url: '/dc/list',
                noParam:true,
                success: function(data){
                    if(data.success) {
                        $scope.dataCenterList = data.data.rows;
                    }
                }
            });
            //获取机柜类型列表数据
            httpLoad.loadData({
                url: '/dict/children',
                method: 'GET',
                data: {
                    value :"RACK_CATEGORY"
                },
                success: function(data){
                    if(data.success) {
                        $scope.dataType = data.data;
                    }
                }
            });
            //获取机柜材质列表数据
            httpLoad.loadData({
                url: '/dict/children',
                method: 'GET',
                data: {
                    value :"RACK_MATERIAL"
                },
                success: function(data){
                    if(data.success) {
                        $scope.dataMaterial = data.data;
                    }
                }
            });
            //获取机房列表信息
            $scope.loadRoomData = function(id){
                var param = [{ param:{dcId:id},sign:'EQ'}];
                httpLoad.loadData({
                    url: '/room/list',
                    noParam: true,
                    data: {
                        params : JSON.stringify(param)
                    },
                    success: function(data){
                        if(data.success) {
                            $scope.roomData = data.data.rows;
                        }
                    }
                });
            };
            if(dcId){
                $scope.inpit_Y= false;
                $scope.dcId = dcId;
                $scope.roomId = roomId;
                $scope.loadRoomData($scope.dcId);
            };

            var editObj = ['name','remark','specs','vendor','productDate','productSn','warranty','type','axisX','axisY','slotAmount','material','dcId','roomId','zoneId','unit'];
            var url = '/rack/create';
            $scope.solt_X = true;

            //如果为编辑，进行赋值
            if(id){
                url = '/rack/modify';
                $scope.modalName = '编辑机柜';
                httpLoad.loadData({
                    url: '/rack/basic',
                    method: 'GET',
                    data: {id: id},
                    success: function(data){
                        if(data.success) {
                            var data = data.data;
                            for(var a in editObj){
                                var attr = editObj[a];
                                $scope[attr] = data[attr];
                            }
                            $scope.inpit_Y= false;
                            $scope.unit = parseInt($scope.unit);
                            $scope.slotAmount = parseInt($scope.slotAmount);
                            // $scope.axisX = parseInt($scope.axisX);
                            // $scope.axisY = parseInt($scope.axisY);
                            $scope.loadRoomData($scope.dcId);
                        }
                    }
                });
            }
            $scope.changeRoom = function (id) {
                if(id)  $scope.inpit_Y= false;
            }
            //卡槽范围
            // $scope.roundFouse = function(){
            //     if(id){
            //         var param = {slotAmount:$scope.slotAmount,id:id};
            //     }else{
            //         return false
            //     }
            //     httpLoad.loadData({
            //         url: '/rack/checkRange',
            //         method: 'GET',
            //         data: param,
            //         success: function(data){
            //             if(data.success) $scope.isRoundFail = false;
            //             else $scope.isRoundFail = true;
            //         }
            //     });
            // };

            //保存按钮
            $scope.ok = function(){
                var param = {};
                for(var a in editObj){
                    var attr = editObj[a];
                    if(attr!='dcId'){
                        param[attr] = $scope[attr];
                    }
                }
                if(id) param.id = id;
                httpLoad.loadData({
                    url: url,
                    data: param,
                    success: function(data){
                        if(data.success) {
                            $scope.pop(id ? LANGUAGE.ASSETS.RACK.EDIT_SUCCESS : LANGUAGE.ASSETS.RACK.ADD_SUCCESS);
                            $modalInstance.close(param);
                        }
                    }
                });
            }
            $scope.cancle = function() {
                $modalInstance.dismiss('cancel');
            };
        }
    ]);
    //删除机柜ctrl
    angular.module('app').controller('delCabinetModalCtrl', ['$scope', '$modalInstance','httpLoad','LANGUAGE','id',
        function($scope, $modalInstance, httpLoad, LANGUAGE, id) {
            $scope.ok = function(){
                httpLoad.loadData({
                    url: '/rack/remove',
                    data:{id: id},
                    success: function(data){
                        if(data.success) {
                            $scope.pop(LANGUAGE.ASSETS.RACK.DEL_SUCCESS);
                            $modalInstance.close();
                        }
                    }
                });
            }
            $scope.cancle = function(){
                $modalInstance.dismiss('cancel');
            };
        }
    ]);
})();